package pw.goyd.game.handler.imp;

import pw.goyd.db.RedisUtil;
import pw.goyd.game.handler.Handler;
import pw.goyd.game.logic.ReadyResponse;
import pw.goyd.game.manager.GameManager;
import pw.goyd.game.server.Log;
import pw.goyd.game.server.LoginList;
import pw.goyd.protocol.model.Response;
import pw.goyd.subscribe.PlayerCustomer;

public class READY extends Handler {
    /**
     * 匹配
     */
    @Override
    public void handle() {
        // 玩家准备完毕
        PlayerCustomer playerCus = LoginList.getPlayer(ctx.getChannel().getId());
        if (playerCus == null) {
            Log.getLogger().error("空指针--->ChannelID[" + ctx.getChannel().getId() + "]");
            return;
        }

        playerCus.setReady(true);

        Response response = new Response();
        ReadyResponse readyResponse = new ReadyResponse();
        readyResponse.setPlayer(playerCus);
        response.setCmd(CMD_READY);
        response.setData(readyResponse.getBytes());
        response.setTime(0);

        RedisUtil.pubRoom(playerCus.getRoomID(), response.getAllBytes());
        GameManager.checkStart(playerCus);  // or Start Game Timer
    }
}
